package com.itlong.cloud.controller.show;

import com.itlong.cloud.POJO.PO.AppTempFaceShowPO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.user.app.IAppShowServiceClient;
import com.itlong.cloud.utils.encrypt.UserTokenUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;

/**
 * <desc>
 *      演示版App人脸识别Controller
 * </desc>
 *
 * @createDate 2018/3/6
 */
@RestController
@RefreshScope
public class AppShowFaceRecognitionController {

    private static final Logger LOG = LoggerFactory.getLogger(AppShowFaceRecognitionController.class);

    @Autowired
    private IAppShowServiceClient iAppShowServiceClient;

    @Value("${" + PlatformConstants.APP_USER_TOKEN_KEY + "}")
    public String APP_USER_TOKEN_KEY;

    /**
     * <desc>
     *      演示人脸识别查询余额
     * <desc/>
     * @param   token 用户接入凭证
     * @return  标准化响应结果
     * @author Biao.R
     * @createDate 2018/3/6
     */
    @ResponseBody
    @RequestMapping("/user/getBalance")
    public MessageVO getBalance(String token) {
        try {
            if (StringUtils.isBlank(token)) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            String userId = UserTokenUtil.tokenDecrypt(token, APP_USER_TOKEN_KEY);
            if (StringUtils.isEmpty(userId)) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            BigDecimal balance = iAppShowServiceClient.getBalanceByUserId(userId);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), balance);
        } catch (Exception e) {
            throw new DataAccessException("【演示版App_人脸识别_getBalance】查询余额失败", e);
        }
    }



    /**
     * <desc>
     *      演示人脸识别更新余额
     * <desc/>
     * @param   token 用户接入凭证
     * @param  num  修改的数据   正数代表充值,负数代表扣费
     * @param  faceId   人脸ID,仅当扣费时上传
     * @return  标准化响应结果
     * @author Biao.R
     * @createDate 2018/3/6
     */
    @ResponseBody
    @RequestMapping("/user/updateBalance")
    public MessageVO updateBalance(String token, String num, String faceId) {
        try {
            if ((StringUtils.isBlank(token) && StringUtils.isEmpty(faceId)) || StringUtils.isEmpty(num)) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            BigDecimal recharge = new BigDecimal(num);
            //扣费时需要上传faceId
            if (recharge.compareTo(BigDecimal.ZERO) == -1 && StringUtils.isEmpty(faceId)) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            String userId = null;
            if (StringUtils.isNotEmpty(token)) {
                userId = UserTokenUtil.tokenDecrypt(token, APP_USER_TOKEN_KEY);
            }
            AppTempFaceShowPO faceShowPO = new AppTempFaceShowPO(userId, faceId, recharge);
            Map<String, Object> returnMap = iAppShowServiceClient.updateBalance(faceShowPO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), returnMap);
        } catch (Exception e) {
            throw new DataAccessException("【演示版App_人脸识别_updateBalance】更新余额失败", e);
        }
    }


}